--*************ANIMATION SUITE****************--
--**************Property of Hxrmn*****************--
--*************Hxrmn@comcast.net*****************--
--DO NOT RE-DISTRIBUTE WITHOUT HXRMN'S CONSENT--
--********************************************--

Controls = {}

function Controls:TieToAnimator(anim)
	local ctrl = {}
	setmetatable(ctrl, self)
	self.__index = self
	ctrl.animator = anim
	
	return ctrl
end

function Controls:GetAnimator()
	return self.animator
end

function Controls:PlayX()
	--AS_Msg("PLAY_PRESS",self:GetAnimator(),AS_MESSAGE_CHAT)
	self:GetAnimator():Play()
	if(CLIENT) then LocalPlayer():ConCommand("AS_PlayX2") end
end

function Controls:Record()
	local function quickRec()
		self:GetAnimator():Record()
	end
	
	local anim = self:GetAnimator()
	local function prtime(t)
		AS_Msg(t,anim,AS_MESSAGE_CHAT)
	end
	
	local t = GetConVarNumber("animation_rec_timer")
	if(t == 0) then
		AS_Msg("Recording Started",anim,AS_MESSAGE_CHAT)
		quickRec()
	else
		prtime("Recording In " .. t .. "...")
		for i=1, t do
			timer.Simple(i,prtime,t-i)
			if(i==t) then
				timer.Simple(i,quickRec)
				timer.Simple(i,prtime,"GO")
			end
		end
	end
	--if(CLIENT) then LocalPlayer():ConCommand("AS_Record") end
end

function Controls:StopX()
	--AS_Msg("STOP_PRESS",self:GetAnimator(),AS_MESSAGE_CHAT)
	self:GetAnimator():Stop()
	if(CLIENT) then LocalPlayer():ConCommand("AS_StopX2") end
end

function Controls:CaptureFrame()
	self:GetAnimator():CaptureFrame()
	if(CLIENT) then LocalPlayer():ConCommand("AS_CaptureFrame") end
end

function Controls:AddEnt(p,fs)
	if(CLIENT) then p = LocalPlayer() end
	local tr = utilx.GetPlayerTrace( p, p:GetAimVector() )
	local trace = util.TraceLine( tr )
	if (!trace.Hit) then return end
	if (!trace.HitNonWorld) then return end
	--if (p:GetPos():Distance(trace.Entity:GetPos()) > 200) then return end
	if(!trace.Entity:IsValid()) then return end
	self:GetAnimator():AddTrack(trace.Entity)
	if(CLIENT and !fs) then LocalPlayer():ConCommand("AS_AddEnt") end
	if(CLIENT and !fs) then AS_UpdateWindow() end
end

function Controls:RemoveEnt(p,fs)
	if(CLIENT) then p = LocalPlayer() end
	local tr = utilx.GetPlayerTrace( p, p:GetAimVector() )
	local trace = util.TraceLine( tr )
	if (!trace.Hit) then return end
	if (!trace.HitNonWorld) then return end
	--if (p:GetPos():Distance(trace.Entity:GetPos()) > 200) then return end
	if(!trace.Entity:IsValid()) then return end
	self:GetAnimator():RemoveTrack(trace.Entity)
	if(CLIENT and !fs) then LocalPlayer():ConCommand("AS_RemoveEnt") end
	if(CLIENT and !fs) then AS_UpdateWindow() end
end

function Controls:DeleteKey(fs)

end

function Controls:GotoTime(t,fs)
	local t = tonumber(t)
	self:GetAnimator():SetTime(t)
	if(CLIENT and !fs) then LocalPlayer():ConCommand("AS_GotoTime " .. t) end
end

function Controls:Clear(fs)
	self:GetAnimator():DeleteArmedTracks()
	if(CLIENT and !fs) then LocalPlayer():ConCommand("AS_Clear") end
end

function Controls:ToggleTrack(id,fs)
	local t = tonumber(id)
	self:GetAnimator():ToggleTrack(id)
	if(CLIENT and !fs) then LocalPlayer():ConCommand("AS_ToggleTrack " .. id) end
end

function Controls:CaptureAndAdvance(fs)
	if(self:GetAnimator():HasActiveTracks()) then
		self:GetAnimator():CaptureFrame()
		self:GetAnimator():AdvanceTime(1,true)
		if(CLIENT and !fs) then LocalPlayer():ConCommand("AS_CapAndAdvance") end
	else
		Error("No armed tracks found.\n")
		self:GetAnimator():Stop()
	end
end